/*
 * SharpSensors.c
 *
 *  Created on: 2012.11.29.
 *      Author: AnnaBaba
 */

#include "SharpSensors.h"

__IO uint16_t SharpSensorValues[3];
__IO uint16_t SharpSensorVoltages[3];

void SS_Pins_Init(void){
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Configure ADC Channel1,2,3 pin as analog input ******************************/
	GPIO_InitStructure.GPIO_Pin = SS_LS_PIN | SS_RS_PIN | SS_FS_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(SS_SS_PORT, &GPIO_InitStructure);
}

void SS_Init(void){
	SS_Pins_Init();
	//ADC1:FrontSensor, ADC2:LeftSensor, ADC3:RightSensor
	ADC_Initialize();
}

void SS_SetDMATarget(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t MemoryBaseAddr)
{
	DMA_Cmd(DMAy_Streamx, DISABLE);
	DMA_MemoryTargetConfig(DMAy_Streamx, MemoryBaseAddr, DMA_Memory_0);
	DMA_Cmd(DMAy_Streamx, ENABLE);
}

void SS_GetValues(void){
	uint8_t i;
	ADC_ChangeChannel(ADC_Channel_11, ADC_Channel_12, ADC_Channel_14);
	SS_SetDMATarget(DMA2_Stream0, (uint32_t)&SharpSensorValues[0]);
	SS_SetDMATarget(DMA2_Stream2, (uint32_t)&SharpSensorValues[1]);
	SS_SetDMATarget(DMA2_Stream1, (uint32_t)&SharpSensorValues[2]);

	DMA_ClearFlag(DMA2_Stream1, DMA_FLAG_TCIF1);
	ADC_SoftwareStartConv(ADC1);
	ADC_SoftwareStartConv(ADC2);
	ADC_SoftwareStartConv(ADC3);
	while(DMA_GetFlagStatus(DMA2_Stream1, DMA_FLAG_TCIF1) == RESET);	//waiting for DMA transfer from ADC3
	for (i = 0; i < 3; i++)
		SharpSensorVoltages[i] = SharpSensorValues[i] *3300/0xFFF;

}


